package com.lzk.auth.controller;
import com.lzk.auth.service.SysUserService;
import com.lzk.common.config.Exception.GuiguException;
import com.lzk.common.result.Result;
import com.lzk.common.utils.MD5;
import com.lzk.model.base.Form;
import com.lzk.model.system.SysUser;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "修改密码")
@RestController
@RequestMapping("/admin/password/changePassword")
public class changePassword {
    @Autowired
    private SysUserService sysUserService;
    @PostMapping("change")
    public Result changePassword(@RequestBody Form form) {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String currentPrincipalName = authentication.getName();
        SysUser user = sysUserService.getUserByUsername(currentPrincipalName);


        String password_db = user.getPassword();
        if(password_db==null){

        }

        String password_input = MD5.encrypt(form.getOldPassword());


        if(!password_db.equals(password_input)) {
            throw new GuiguException(201,"密码错误");
        }

        String passwordMD5 = MD5.encrypt(form.getNewPassword());
        user.setPassword(passwordMD5);
        sysUserService.updateById(user);
        return Result.ok();

    }

}
